/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.bean.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.persistence.manager.PersistenceManager;
import com.genia.toolbox.projects.toolbox_basics_project.bean.InitializationParameterHandler;
import com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicText;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nStringParameter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Parameter;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nDynamicTextImpl;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.ParameterManager;

/**
 * Implementation of {@link PersistableApplicationParameterHandler} for
 * {@link I18nDynamicText}.
 */
public class I18nStringPersistableApplicationParameterHandler
    implements InitializationParameterHandler<I18nDynamicText>
{

  /**
   * the {@link Set} of {@link Class} that this handler handle.
   */
  @SuppressWarnings("unchecked")
  private static final Set<Class<? extends I18nDynamicText>> HANDLING_CLASSES = new HashSet<Class<? extends I18nDynamicText>>(Arrays.asList(I18nDynamicText.class));

  /**
   * the {@link ParameterManager} to use.
   */
  private ParameterManager parameterManager;

  /**
   * the {@link PersistenceManager} to use.
   */
  private PersistenceManager persistenceManager;



  /**
   * returns the {@link Set} of {@link Class} that this handler handle.
   * 
   * @return the {@link Set} of {@link Class} that this handler handle or
   *         <code>null</code> if this handler is the default one
   * @see com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameterHandler#getHandlingClasses()
   */
  public Set<Class<? extends I18nDynamicText>> getHandlingClasses()
  {
    return HANDLING_CLASSES;
  }



  /**
   * intialize the given {@link PersistableApplicationParameter}.
   * 
   * @param persistableApplicationParameter
   *          the {@link PersistableApplicationParameter} to initialize
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.bean.PersistableApplicationParameterHandler#initValue(com.genia.toolbox.projects.toolbox_basics_project.bean.impl.PersistableApplicationParameter)
   */
  @Transactional(rollbackFor = PersistenceException.class)
  public void initValue(PersistableApplicationParameter<I18nDynamicText> persistableApplicationParameter)
      throws PersistenceException
  {
    Parameter parameter = getParameterManager().getParameter(persistableApplicationParameter.getReferenceName());
    if (parameter != null && !(parameter instanceof I18nStringParameter)) {
      getParameterManager().delete(parameter);
      parameter = null;
    }
    if (parameter != null) {
      persistableApplicationParameter.setValue(getPersistenceManager().getCrossSessionManager().getCrossSessionObject(((I18nStringParameter) parameter).getI18nValue()));
    }
    if (persistableApplicationParameter.getValue() == null) {
      persistableApplicationParameter.setValue(new I18nDynamicTextImpl());
    }
  }



  /**
   * getter for the parameterManager property.
   * 
   * @return the parameterManager
   */
  public ParameterManager getParameterManager()
  {
    return parameterManager;
  }



  /**
   * setter for the parameterManager property.
   * 
   * @param parameterManager
   *          the parameterManager to set
   */
  public void setParameterManager(ParameterManager parameterManager)
  {
    this.parameterManager = parameterManager;
  }



  /**
   * getter for the persistenceManager property.
   * 
   * @return the persistenceManager
   */
  public PersistenceManager getPersistenceManager()
  {
    return persistenceManager;
  }



  /**
   * setter for the persistenceManager property.
   * 
   * @param persistenceManager
   *          the persistenceManager to set
   */
  public void setPersistenceManager(PersistenceManager persistenceManager)
  {
    this.persistenceManager = persistenceManager;
  }

}
